package com.pickup.rabbitMQ.config;


import com.pickup.rabbitMQ.listener.RabbitMqListener;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitMqConfig {

    @Autowired
    private RabbitMqListener rabbitMqListener;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    // 该注解修饰的方法在构造方法执行之后，init方法执行之前执行
    public void rabbitMqInit(){
        // 配置Confirm回调
        rabbitTemplate.setConfirmCallback(rabbitMqListener);
        // 配置Return回调
        rabbitTemplate.setReturnCallback(rabbitMqListener);
    }

    /**
     * @Title 创建2个队列
     * @Description 创建2个队列
     **/
    @Bean
    public Queue felixQueueA(){
        return new Queue("ChatMessage0");
    }
 
    @Bean
    public Queue felixQueueB(){
        return new Queue("ChatMessage1");
    }
 
    /**
     * @Title 声明一个Direct类型的交换机
     * @Description 声明一个Direct类型的交换机
     **/
    @Bean
    DirectExchange directExchange(){
        return new DirectExchange("chat");
    }
 
    /**
     * @Title 将上面的2个队列绑定到Direct交换机
     * @Description 在绑定的时候指定BindingKey
     **/
    @Bean
    Binding bindExchangeA(Queue felixQueueA, DirectExchange directExchange){
        return BindingBuilder.bind(felixQueueA).to(directExchange).with("0");
    }
 
    @Bean
    Binding bindExchangeB(Queue felixQueueB,DirectExchange directExchange){
        return BindingBuilder.bind(felixQueueB).to(directExchange).with("1");
    }
}